package com.yxw.live_vod_backend_boot.controller;

import com.yxw.live_vod_backend_boot.dto.ProductAddDTO;
import com.yxw.live_vod_backend_boot.dto.ProductDeleteDTO;
import com.yxw.live_vod_backend_boot.dto.ProductQueryDTO;
import com.yxw.live_vod_backend_boot.dto.ProductUpdateDTO;
import com.yxw.live_vod_backend_boot.service.ProductService;
import com.yxw.live_vod_backend_boot.vo.ProductSpecificationVO;
import com.yxw.live_vod_backend_boot.vo.ProductVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.helper.ResultHelper;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理 Controller控制层
 *
 * @author xiexuxin
 * @date 2023-12-07
 */
@RestController
@Api(value = "商品管理", tags = "商品管理")
@Slf4j
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("添加商品")
    @PostMapping("add_product")
    public BaseResult addProduct(@RequestBody @Validated ProductAddDTO addDTO) {
        List<ProductVO> voList = productService.addProduct(addDTO);
        return BaseResult.doSuccess(voList, "添加成功");
    }

    @ApiOperation("删除商品")
    @PostMapping("delete_product")
    public BaseResult deleteProduct(@RequestBody ProductDeleteDTO dto) {
        productService.deleteProduct(dto);
        return BaseResult.doSuccess(null, "删除成功");
    }

    @ApiOperation("修改商品")
    @PostMapping("update_product")
    public BaseResult updateProduct(@RequestBody ProductUpdateDTO updateDTO) {
        productService.updateProduct(updateDTO);
        return BaseResult.doSuccess(null, "修改成功");
    }

    @ApiOperation("根据Id获取商品详情")
    @PostMapping("get_product_by_id")
    public BaseResult<ProductVO> getProductById(@RequestParam("productId") String productId) {
        ProductVO vo = productService.getProductById(productId);
        return BaseResult.doSuccess(vo, "查询成功");
    }

    @ApiOperation("获取商品列表")
    @PostMapping("get_product_list")
    public BaseResult<PageVO<ProductVO>> getProductList(@RequestBody @Validated ProductQueryDTO queryDTO) {
        PageVO<ProductVO> pageVO = productService.getProductList(queryDTO);
        return ResultHelper.fillWithSuccess(pageVO);
    }

    @ApiOperation("根据商品系列号获取商品列表")
    @PostMapping("get_product_by_num")
    public BaseResult getProductByNum(@RequestParam("productSeriesNum") String productSeriesNum) {
        List<ProductVO> list = productService.getProductByNum(productSeriesNum);
        return BaseResult.doSuccess(list, "查询成功");
    }

    @ApiOperation("商品上架/下架")
    @PostMapping("is_put_away_by_id")
    public BaseResult isPutAwayById(@RequestParam("productId") String productId,
                                    @RequestParam("productStatus") Integer productStatus) {
        productService.isPutAwayById(productId,productStatus);
        return BaseResult.doSuccess(null, "设置成功");
    }

    @ApiOperation("将redis中的库存相关取出并保存到数据库")
    @PostMapping("get_product_stock")
    public BaseResult getProductStock(@RequestParam("productId") String productId) {
        productService.getProductStock(productId);
        return BaseResult.doSuccess(null, "查询成功");
    }

    @ApiOperation("根据商品ID获取对应商品规格列表信息")
    @PostMapping("get_product_specification_by_id")
    public BaseResult<List<ProductSpecificationVO>> getProductSpecificationById(@RequestParam("productId") String productId) {
        List<ProductSpecificationVO> voList = productService.getProductSpecificationById(productId);
        return BaseResult.doSuccess(voList, "查询成功");
    }
}
